using SmartLink.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SmartLink.Domain.ExpandEntity.Base;
using SmartLink.Domain.IServices;
using SmartLink.Infrastructure.Cache;
using SmartLink.DTOS.SysBasic;
using SmartLink.DTOS;
using SmartLink.Repository.Base;
using SmartLink.Infrastructure;

namespace SmartLink.Domain.Services
{
    public class SysBasicServices : ISysBasicServices
    {
        private ISYS_DictionaryRepo _dictionaryRepository;
        public SysBasicServices(ISYS_DictionaryRepo dictionaryRepository)
        {
            _dictionaryRepository = dictionaryRepository;
        }

        public OperationResult AddDictionary(SYS_Dictionary dictionary)
        {
            var id = _dictionaryRepository.Add(dictionary);
            if (id > 0)
                return new OperationResult(OperationResultType.Success);
            else
                return new OperationResult(OperationResultType.ParamError,
                    string.Format("指定字典的字典项已存在。(CategoryValue:{0}，DictionaryItemValue:{1})",
                    dictionary.CategoryValue, dictionary.DictionaryItemValue));
        }

        /// <summary>
        /// 获取字典
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public List<SYS_Dictionary> DictionaryList(string catogaryValue)
        {
            var dic = DictionryCache.GetDicCache(catogaryValue);
            if (dic != null)
                return dic.ToList();
            else
            {
                List<SYS_Dictionary> curDic = _dictionaryRepository.DictionaryList(catogaryValue);
                if (curDic != null && curDic.Count>0 )
                    DictionryCache.SetDicCache(catogaryValue, curDic);
                return curDic;
            }
        }


        public string GetDescriptionByValue(string catogaryValue, int itemValue)
        {
            string itemName = "";
            List<SYS_Dictionary> dicItems = DictionaryList(catogaryValue);
            if (dicItems != null && dicItems.Count > 0)
            {
                var selected = dicItems.Where(item => item.DictionaryItemValue == itemValue);
                if (selected != null && selected.Count() > 0)
                    itemName = selected.ToList()[0].DictionaryItemName;
            }

            return itemName;
        }

        public  int GetValueByDesc(string catogaryValue, string itemName)
        {
            var itemValue = -1;
            List<SYS_Dictionary> dicItems =DictionaryList(catogaryValue);
            if (dicItems != null && dicItems.Count > 0)
            {
                var selected = dicItems.Where(item => item.DictionaryItemName == itemName);
                if (selected != null && selected.Count() > 0)
                    itemValue = selected.ToList()[0].DictionaryItemValue;
            }

            return itemValue;
        }

        /// <summary>
        /// 返回ID
        /// </summary>
        /// <param name="catogaryValue"></param>
        /// <param name="itemName"></param>
        /// <returns></returns>
        public int GetIDValueByDesc(string catogaryValue, string itemName)
        {
            var itemValue = -1;
            List<SYS_Dictionary> dicItems = DictionaryList(catogaryValue);
            if (dicItems != null && dicItems.Count > 0)
            {
                var selected = dicItems.Where(item => item.DictionaryItemName == itemName);
                if (selected != null && selected.Count() > 0)
                    itemValue = selected.ToList()[0].ID;
            }

            return itemValue;
        }

        public List<AreaExpand> GetAreaList()
        {
            return _dictionaryRepository.GetAreaList();
        }
    }
}
